// -*- C++ -*-

#include <pthread.h>
#include <boost/utility.hpp> //for boost::noncopyable
#include <boost/function.hpp>
#include <boost/bind.hpp>

#ifndef __THREAD_H__
#define __THREAD_H__

class ThreadLoop : boost::noncopyable {
public:
    ThreadLoop(boost::function<void* (const int *)> f) :m_func(f){
        m_running = 0;
    }
    int start() {
        if (m_running) return 0;
        m_running = 1;
        return pthread_create(&m_thr_id, NULL, &call_method, this);
    }
    int stop(){
        if (!m_running) return 0;
        m_running = 0;
        return pthread_join(m_thr_id, NULL);
    }
    const int is_running() {return m_running;}
protected:
    boost::function<void* (const int *)> m_func;
    pthread_t m_thr_id;
    int m_running;
    static void* call_method(void* pthr) {
        return (((ThreadLoop*)pthr)->m_func)(&(((ThreadLoop*)pthr)->m_running));
    }
};

#endif
